﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views;
using XCI.Windows;

namespace DevExpress.ApplicationUI.Demos
{
    [WidgetCaption("时钟Widget")]
    public partial class Clock : UserControl, IWidget
    {
        Timer timer = new Timer();
        public Clock()
        {
            InitializeComponent();
            timer.Interval = 1000;
            timer.Tick += OnTick;
            timer.Start();
            OnTick(null, null);
        }

        public BaseDocument CreateDocument()
        {
            var doc = new XtraBars.Docking2010.Views.Widget.Document
            {
            };
            return doc;
        }

        public Control CreateInstance(Dictionary<string, string> paramMap)
        {
            return new Clock();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
                DisableTimer();
            }
            base.Dispose(disposing);
        }
        void DisableTimer()
        {
            timer.Stop();
            timer = null;
        }
        void OnTick(object sender, EventArgs e)
        {
            if (IsDisposed)
            {
                DisableTimer();
                return;
            }
            System.DateTime currentDate = System.DateTime.Now;
            labelControl1.Text = "<b>" + string.Format("{0:T}", currentDate) + "</b><br><size=10>" + currentDate.ToString("D");
        }
    }
}
